using MilkShake;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
	public static Player Instance;

	public CampaignGame campaignGame;

	public float MaxMoveX = 30f;

	public bool controllable;

	public PlayerBody body;

	public AudioSource audioSource;

	public Joystick joystick;

	public float xInput;

	public float yInput;

	public Weapon weapon;

	public Weapon primaryWeapon;

	public Weapon secondaryWeapon;

	public MeleeWeapon meleeWeapon;

	public Vector3 lastPointerPos;

	private bool onShoot;

	public Rigidbody2D rb;

	public ShakePreset shake;

	public Animator gameAnimator;

	public int zombieContactCount;

	public bool isDead;

	public bool isPlacingItem;

	public bool releaseLeftHand;

	public bool releaseRightHand;

	public int kickCount;

	private float loadWeight;

	public float speed;

	public float strength;

	public int kickTotal;

	public AudioClip[] hurtAudios;

	public AudioClip[] puchAudios;

	public int groundLayer = 28;

	public Tool placingTool;

	[Header("Runtime")]
	public bool changingWeapon;

	private void Awake()
	{
		Instance = this;
	}

	private void Start()
	{
		EquipAll();
		if (campaignGame.isPlayground)
		{
			kickTotal = DO.Playground.playerKickTotal;
		}
		else
		{
			kickTotal = DO.Campaign.playerKickTotal;
		}
	}

	public void EquipSuit()
	{
		if (body != null)
		{
			Object.Destroy(body.gameObject);
		}
		if (base.transform.childCount > 0)
		{
			Object.Destroy(base.transform.GetChild(0).gameObject);
		}
		string text = ((!campaignGame.isPlayground) ? DO.Campaign.suit : DO.Playground.suit);
		body = Object.Instantiate((GameObject)Resources.Load("Suits/" + text), base.transform).transform.GetChild(0).GetComponent<PlayerBody>();
		if (campaignGame.isPlayground)
		{
			strength = DO.Playground.playerStrength + body.strength;
			speed = DO.Playground.playerSpeed + body.speed;
			kickTotal = DO.Playground.playerKickTotal + body.kick;
		}
		else
		{
			strength = DO.Campaign.playerStrength + body.strength;
			speed = DO.Campaign.playerSpeed + body.speed;
			kickTotal = DO.Campaign.playerKickTotal + body.kick;
		}
	}

	public void EquipAll()
	{
		EquipSuit();
		PickupWeapons();
		EquipWeapon();
	}

	public void PickupWeapons()
	{
		string text;
		string text2;
		string text3;
		if (campaignGame.isPlayground)
		{
			text = DO.Playground.primaryWeapon;
			text2 = DO.Playground.secondaryWeapon;
			text3 = DO.Playground.meleeWeapon;
		}
		else
		{
			text = DO.Campaign.primaryWeapon;
			text2 = DO.Campaign.secondaryWeapon;
			text3 = DO.Campaign.meleeWeapon;
		}
		if (text != "" && text != null)
		{
			primaryWeapon = Object.Instantiate(CampaignGame.itemCache[text].prefab, body.primaryWeaponHolder.transform).GetComponent<Weapon>();
			PutBack(primaryWeapon);
			primaryWeapon.player = this;
			primaryWeapon.name = primaryWeapon.name.Replace("(Clone)", "");
			loadWeight += CampaignGame.itemCache[text].weight;
		}
		if (text2 != "" && text2 != null)
		{
			secondaryWeapon = Object.Instantiate(CampaignGame.itemCache[text2].prefab, body.secondaryWeaponHolder.transform).GetComponent<Weapon>();
			PutBack(secondaryWeapon);
			secondaryWeapon.player = this;
			secondaryWeapon.name = secondaryWeapon.name.Replace("(Clone)", "");
			loadWeight += CampaignGame.itemCache[text2].weight;
		}
		if (text3 != "" && text3 != null)
		{
			meleeWeapon = Object.Instantiate(CampaignGame.itemCache[text3].prefab, body.meleeWeaponHolder.transform).GetComponent<MeleeWeapon>();
			meleeWeapon.player = this;
			meleeWeapon.animator.enabled = false;
			meleeWeapon.name = meleeWeapon.name.Replace("(Clone)", "");
			loadWeight += CampaignGame.itemCache[text3].weight;
		}
	}

	public void EquipWeapon()
	{
		if ((bool)primaryWeapon)
		{
			EquipPrimaryWeapon();
		}
		else if ((bool)secondaryWeapon)
		{
			EquipSecondaryWeapon();
		}
		else if ((bool)meleeWeapon)
		{
			EquipMeleeWeapon();
		}
	}

	public void EquipPrimaryWeapon()
	{
		EquipWeaponByHolder(body.primaryWeaponHolder);
	}

	public void EquipSecondaryWeapon()
	{
		EquipWeaponByHolder(body.secondaryWeaponHolder);
	}

	public void EquipMeleeWeapon()
	{
		EquipWeaponByHolder(body.meleeWeaponHolder);
	}

	public void SwapWeapon()
	{
		if (!isDead)
		{
			if (weapon == primaryWeapon)
			{
				EquipWeaponByHolder(body.secondaryWeaponHolder);
			}
			else
			{
				EquipWeaponByHolder(body.primaryWeaponHolder);
			}
		}
	}

	public void SwapMeleeWeapon()
	{
		if (!isDead)
		{
			EquipWeaponByHolder(body.meleeWeaponHolder);
		}
	}

	public void PutBack(Weapon weapon)
	{
		weapon.Stop();
		weapon.enabled = false;
		weapon.animator.enabled = false;
		weapon.magazine.rb.isKinematic = true;
		weapon.leftHandHandler.SetActive(value: false);
		Transform obj = weapon.transform.Find("Body");
		Transform transform = weapon.transform.Find("Body/PutbackPos");
		weapon.transform.localPosition = Vector2.zero;
		obj.transform.localPosition = Vector3.zero - transform.localPosition;
		weapon.transform.localRotation = transform.localRotation;
		obj.localRotation = Quaternion.Euler(0f, 0f, 0f);
	}

	public void PutBackWeapon()
	{
		if ((bool)weapon)
		{
			if ((bool)secondaryWeapon && weapon.gameObject == secondaryWeapon.gameObject)
			{
				weapon.transform.SetParent(body.secondaryWeaponHolder.transform);
			}
			else
			{
				weapon.transform.SetParent(body.primaryWeaponHolder.transform);
			}
			PutBack(weapon);
		}
		else if ((bool)meleeWeapon)
		{
			meleeWeapon.transform.SetParent(body.meleeWeaponHolder.transform);
			meleeWeapon.enabled = false;
			meleeWeapon.animator.enabled = false;
		}
	}

	public void EquipWeaponByHolder(GameObject holder)
	{
		if (isPlacingItem || holder.transform.childCount == 0)
		{
			return;
		}
		GameObject gameObject = holder.transform.GetChild(0).gameObject;
		if (gameObject == weapon || gameObject == meleeWeapon)
		{
			return;
		}
		PutBackWeapon();
		Weapon component = gameObject.GetComponent<Weapon>();
		if ((bool)component)
		{
			body.RestArm();
			weapon = component;
			weapon.transform.SetParent(body.gunPivot.transform);
			weapon.transform.localPosition = Vector2.zero;
			weapon.transform.localRotation = Quaternion.Euler(0f, 0f, -45f);
			weapon.enabled = true;
			weapon.animator.enabled = true;
			weapon.leftHandHandler.SetActive(value: true);
			weapon.player = this;
			if ((bool)meleeWeapon)
			{
				meleeWeapon.animator.Rebind();
				meleeWeapon.Sleep();
				meleeWeapon = null;
			}
		}
		else
		{
			meleeWeapon = gameObject.GetComponent<MeleeWeapon>();
			meleeWeapon.transform.SetParent(body.meleeWeaponPivot.transform);
			meleeWeapon.enabled = true;
			meleeWeapon.animator.enabled = true;
			weapon = null;
			meleeWeapon.Weakup();
			meleeWeapon.animator.Play("Change");
		}
		if (CampaignGame.itemCache[gameObject.name].weight > 0f)
		{
			speed = Mathf.Max(1f, (campaignGame.isPlayground ? DO.Playground.playerSpeed : DO.Campaign.playerSpeed) + body.speed - CampaignGame.itemCache[gameObject.name].weight / 10f);
		}
		body.animator.SetFloat("speed", speed);
		changingWeapon = true;
	}

	public void ReloadWeapon()
	{
		if ((bool)weapon)
		{
			weapon.Reload();
		}
	}

	public void DeadBodyShake()
	{
		body.transform.Find("ChestRb").GetComponent<Rigidbody2D>().AddForce(Random.Range(30f, 60f) * Vector2.up);
		Invoke("DeadBodyShake", Random.Range(0.5f, 1f));
	}

	private void Update()
	{
		if (!isDead)
		{
			if (DO.Campaign.playerHealth <= 0f)
			{
				Die();
				campaignGame.Gameover();
			}
			LookAtLastPointerPos();
			Movement();
		}
	}

	private void LateUpdate()
	{
		if (!isDead)
		{
			HoldTheWeapon();
			if (isPlacingItem && (bool)meleeWeapon && (bool)placingTool && !placingTool.thrownObject)
			{
				meleeWeapon.transform.localPosition = Vector3.zero;
				meleeWeapon.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
			}
		}
	}

	public void Die()
	{
		isDead = true;
		if ((bool)weapon)
		{
			weapon.Stop();
		}
		Rigidbody2D[] rbs = body.rbs;
		foreach (Rigidbody2D rigidbody2D in rbs)
		{
			if ((bool)rigidbody2D)
			{
				rigidbody2D.gameObject.layer = groundLayer;
			}
		}
		Object.Destroy(body.animator);
		body.ikManager.enabled = false;
		rb.constraints = RigidbodyConstraints2D.FreezeAll;
		rbs = body.rbs;
		foreach (Rigidbody2D rigidbody2D2 in rbs)
		{
			rigidbody2D2.isKinematic = false;
			if (rigidbody2D2.name == "HeadRb")
			{
				rigidbody2D2.AddForce(Random.Range(2f, 3f) * 100f * Vector2.left);
			}
		}
		Object.Destroy(body.leftHandJoint);
		Object.Destroy(body.rightHandJoint);
		Object.Destroy(body.kickCollider.gameObject);
		if ((bool)meleeWeapon)
		{
			Object.Destroy(meleeWeapon.gameObject);
		}
		else
		{
			Object.Destroy(weapon.gameObject);
		}
		PlayerTargetJoint[] componentsInChildren = GetComponentsInChildren<PlayerTargetJoint>();
		foreach (PlayerTargetJoint obj in componentsInChildren)
		{
			Object.Destroy(obj.tj);
			Object.Destroy(obj);
		}
	}

	public void Movement()
	{
		if (body.animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerKick"))
		{
			body.animator.SetFloat("xInput", 0f);
			body.animator.SetFloat("yInput", 0f);
		}
		else
		{
			if (isPlacingItem)
			{
				return;
			}
			if (campaignGame.storyState < 1)
			{
				xInput = 0f;
				yInput = 0f;
				if (joystick.Horizontal != 0f)
				{
					xInput = joystick.Horizontal + Input.GetAxis("Horizontal");
				}
				if (joystick.Vertical != 0f)
				{
					yInput = joystick.Vertical;
				}
				if (Input.GetAxis("Horizontal") != 0f)
				{
					xInput = Input.GetAxis("Horizontal");
				}
				if (Input.GetAxis("Vertical") != 0f)
				{
					yInput = Input.GetAxis("Vertical");
				}
			}
			if (base.transform.position.x > MaxMoveX && xInput > 0f)
			{
				xInput = 0f;
				MaxXReached();
			}
			body.animator.SetFloat("xInput", xInput);
			body.animator.SetFloat("yInput", yInput);
			float num = ((!(xInput < 0f)) ? speed : (speed * 0.8f));
			if (zombieContactCount > 0)
			{
				num = Mathf.Max(num - (float)zombieContactCount, 0.1f);
			}
			base.transform.position = new Vector2(base.transform.position.x + xInput * num * 1.2f * Time.deltaTime, base.transform.position.y + yInput * num * 0.4f * Time.deltaTime);
		}
	}

	public void MaxXReached()
	{
	}

	public void Say()
	{
	}

	public void StartAttack()
	{
		if ((bool)weapon)
		{
			if (!isPlacingItem || weapon.autoCocking)
			{
				onShoot = true;
				if (!weapon.onReload && weapon.ammo == 0)
				{
					weapon.Reload();
				}
				else
				{
					weapon.OpenFire();
				}
			}
		}
		else if ((bool)meleeWeapon && !isPlacingItem)
		{
			meleeWeapon.Attack();
		}
	}

	public void StopAttack()
	{
		if ((bool)weapon)
		{
			if (!weapon.onReload && weapon.ammo == 0)
			{
				weapon.Reload();
			}
			onShoot = false;
			weapon.Stop();
		}
		else
		{
			_ = (bool)meleeWeapon;
		}
	}

	private void HoldTheWeapon()
	{
		if ((bool)weapon)
		{
			if (!releaseLeftHand)
			{
				body.leftHandIK.transform.position = weapon.leftHandHandler.transform.position;
			}
			else
			{
				body.leftHandIK.transform.position = body.leftHandJoint.transform.position;
			}
			if (!releaseRightHand)
			{
				body.rightHandIK.transform.position = weapon.rightHandHandler.transform.position;
				float num = strength;
				if (changingWeapon)
				{
					num = speed * 2f;
				}
				weapon.transform.localRotation = Quaternion.Slerp(weapon.transform.localRotation, Quaternion.Euler(0f, 0f, 0f), num * Time.deltaTime);
				weapon.transform.position = body.gunPivot.transform.position;
			}
			else
			{
				body.rightHandIK.transform.position = body.rightHandJoint.transform.position;
			}
		}
		else if ((bool)meleeWeapon)
		{
			if (!releaseLeftHand)
			{
				body.leftHandIK.transform.position = meleeWeapon.leftHandHandle.transform.position;
			}
			else
			{
				body.leftHandIK.transform.position = body.leftHandJoint.transform.position;
			}
			if (!releaseRightHand)
			{
				body.rightHandIK.transform.position = meleeWeapon.rightHandHandle.transform.position;
			}
			else
			{
				body.rightHandIK.transform.position = body.rightHandJoint.transform.position;
			}
		}
		if (!weapon || releaseRightHand)
		{
			body.rightHand.transform.localRotation = Quaternion.Euler(0f, 0f, -20f);
		}
		else
		{
			body.rightHand.transform.rotation = weapon.rightHandHandler.transform.rotation * Quaternion.Euler(0f, 0f, 90f);
		}
	}

	public void LookAtLastPointerPos()
	{
		if ((bool)weapon && onShoot && !(lastPointerPos == Vector3.zero))
		{
			lastPointerPos.z = Mathf.Abs(Camera.main.transform.position.z);
			Vector3 vector = Camera.main.ScreenToWorldPoint(lastPointerPos);
			vector.x -= body.gunPivot.transform.position.x;
			vector.y = vector.y - body.gunPivot.transform.position.y - weapon.offset.y;
			float num = Mathf.Atan2(vector.y, vector.x) * 57.29578f;
			if (!(num > 30f) && !(num < -90f))
			{
				body.gunPivot.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, num));
			}
		}
	}

	public void UpdatePointerPos(BaseEventData data)
	{
		PointerEventData pointerEventData = data as PointerEventData;
		lastPointerPos = pointerEventData.position;
	}

	public void Hurt(float damge)
	{
		if (!isDead && !CampaignGame.exited)
		{
			if (!CampaignGame.Instance.isPlayground)
			{
				damge *= (float)Mathf.Max(DO.Campaign.day / 200, 1);
			}
			if (body.armor > 0f)
			{
				body.armor = Mathf.Max(body.armor - damge, 0f);
			}
			else if (!campaignGame.isPlayground)
			{
				DO.Campaign.playerHealth -= damge;
			}
			Shaker.ShakeAll(shake);
			_ = 20f;
			if (DO.Campaign.playerHealth < 50f)
			{
				audioSource.clip = hurtAudios[Random.Range(0, hurtAudios.Length)];
				audioSource.Play();
			}
			gameAnimator.Play("HurtPlayer");
		}
	}

	public void Kick()
	{
		if (!isDead && !isPlacingItem && !body.animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerKick"))
		{
			kickCount = 0;
			body.animator.Play("PlayerKick");
		}
	}

	public void PuchAudio()
	{
		audioSource.PlayOneShot(puchAudios[Random.Range(0, puchAudios.Length)], 0.5f);
	}

	public void ThrownItem(int index)
	{
		if (CampaignGame.Instance.player.isDead || CampaignGame.Instance.player.isPlacingItem || CampaignGame.Instance.itemListView.transform.childCount <= index || ((bool)weapon && weapon.onReload))
		{
			return;
		}
		string key = CampaignGame.Instance.itemListView.transform.GetChild(index).name;
		if (CampaignGame.itemCache.ContainsKey(key))
		{
			Item item = CampaignGame.itemCache[key];
			if ((bool)item.prefab.GetComponent<Tool>() && (bool)item.prefab.GetComponent<Tool>().thrownObject)
			{
				CampaignGame.Instance.CancelPlaceTools();
				CampaignGame.Instance.ChoosePlaceTools(CampaignGame.Instance.itemListView.transform.GetChild(index).GetComponent<Button>());
				CampaignGame.Instance.ConfirmPlaceTools();
			}
		}
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (isDead || !(collision.gameObject.tag == "Ground"))
		{
			return;
		}
		if ((bool)primaryWeapon)
		{
			int num = collision.gameObject.layer + Ground.groundTotal + primaryWeapon.bulletShell.colliderLayer;
			if (num < 24)
			{
				num = 24;
			}
			if (num > 31)
			{
				num = 31;
			}
			primaryWeapon.bulletShell.gameObject.layer = num;
			if ((bool)primaryWeapon.magazine)
			{
				primaryWeapon.magazine.gameObject.layer = collision.gameObject.layer + Ground.groundTotal;
			}
		}
		if ((bool)secondaryWeapon)
		{
			int num2 = collision.gameObject.layer + Ground.groundTotal + secondaryWeapon.bulletShell.colliderLayer;
			if (num2 < 24)
			{
				num2 = 24;
			}
			if (num2 > 31)
			{
				num2 = 31;
			}
			secondaryWeapon.bulletShell.gameObject.layer = num2;
			if ((bool)secondaryWeapon.magazine)
			{
				secondaryWeapon.magazine.gameObject.layer = collision.gameObject.layer + Ground.groundTotal;
			}
		}
		groundLayer = collision.gameObject.layer + Ground.groundTotal;
	}
}
